<?php

namespace app\common\model;

use app\common\model\BaseModel;
use think\model\concern\SoftDelete;

class Order extends BaseModel
{

    use SoftDelete;

    

    // 表名
    protected $name = 'order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text',
        'deposit_status_text',
        'start_time_text',
        'createtime_text',
        'end_time_text',
        'refund_time_text',
        'time_end_text',
        'send_type_text'
    ];
    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : 1);
        return is_numeric($value) ? self::fetchStatus($value) : $value;
    }

    public function getSendTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['send_type']) ? $data['send_type'] : 1);
        return is_numeric($value) ? self::fetchSendType($value) : $value;
    }

    public function getDepositStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['deposit_status']) ? $data['deposit_status'] : 1);
        return is_numeric($value) ? self::fetchDepositStatus($value) : $value;
    }
    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getTimeEndTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['refund_time']) ? $data['refund_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getRefundTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['refund_time']) ? $data['refund_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function user(){
        return $this->hasOne('user', 'id', 'user_id');
    }
    public function users(){
        return $this->hasOne('user', 'id', 'user_id')->bind(['username','avatar','truename','id_card']);
    }

    public function continue(){
        return $this->hasMany('order_continue', 'order_id', 'id')->where(['is_delete' => 1, 'pay_status' => ['in', '1,2'], 'continue_status' => ['in', '1,2']]);
    }

    //所有换车订单
    public function orderChange(){
        return $this->hasMany('order_change', 'order_id', 'id')->field('id,order_id,merchant_car_id,type,out_trade_no,price,all_money,diff_money,deposit_status,other_number,deposit,handle_fee,rental_money,last_all_money')->where('is_delete', 1)->order('createtime', 'desc');
    }

    //已支付和已退款的换车订单
    public function orderChangePay(){
        return $this->hasMany('order_change', 'order_id', 'id')->field('id,order_id,merchant_car_id,type,out_trade_no,price,all_money,diff_money,deposit_status,other_number,deposit,handle_fee,rental_money,last_all_money')->where("is_delete=1 and pay_status in (2,4)")->order('createtime', 'desc');
    }

    //已支付的有偿换车订单
    public function orderChangePayed(){
        return $this->hasMany('order_change', 'order_id', 'id')->field('id,order_id,out_trade_no,all_money,diff_money,deposit_status,last_all_money')->where("is_delete=1 and type=2 and pay_status in (2) and diff_money>0")->order('createtime', 'desc');;
    }

    //待支付的换车订单
    public function orderChangeUnpay(){
        return $this->hasOne('order_change', 'order_id', 'id')->field('id,order_id,out_trade_no,all_money,diff_money,deposit_status,last_all_money')->where("is_delete=1 and type=2 and pay_status in (1)")->order('createtime', 'desc');;
    }

    //所有续租订单
    public function orderContinue(){
        return $this->hasMany('order_continue', 'order_id', 'id')->field('id,order_id,out_trade_no,all_money,deposit_status,start_time,end_time,last_all_money')->where("is_delete=1")->order('createtime', 'desc');
    }

    //已支付和已退款的续租订单
    public function orderContinuePay(){
        return $this->hasMany('order_continue', 'order_id', 'id')->field('id,order_id,out_trade_no,all_money,deposit_status,start_time,end_time,last_all_money')->where("is_delete=1 and pay_status in (2,4) and continue_status in (2)")->order('createtime', 'desc');;
    }

    //已支付的续租订单
    public function orderContinuePayed(){
        return $this->hasMany('order_continue', 'order_id', 'id')->field('id,order_id,out_trade_no,all_money,deposit_status,start_time,end_time,last_all_money')->where("is_delete=1 and pay_status in (2) and continue_status in (2)")->order('createtime', 'desc');;
    }

    //待支付的续租订单
    public function orderContinueUnpay(){
        return $this->hasOne('order_continue', 'order_id', 'id')->field('id,order_id,out_trade_no,all_money,deposit_status,start_time,end_time,last_all_money')->where("is_delete=1 and pay_status in (1) and continue_status in (1,2)")->order('createtime', 'desc');;
    }

    //所有当面付订单
    public function orderFace(){
        return $this->hasMany('order_face', 'order_id', 'id')->field('id,order_id,out_trade_no,all_money,last_all_money')->where("is_delete=1")->order('createtime', 'desc');
    }

    //已支付和已退款的当面付订单
    public function orderFacePay(){
        return $this->hasMany('order_face', 'order_id', 'id')->field('id,order_id,out_trade_no,all_money,last_all_money')->where("is_delete=1 and status in (2,4)")->order('createtime', 'desc');;
    }

    //已支付当面付订单
    public function orderFacePayed(){
        return $this->hasMany('order_face', 'order_id', 'id')->field('id,order_id,out_trade_no,all_money,last_all_money')->where("is_delete=1 and status in (2)")->order('createtime', 'desc');;
    }

    //待支付的当面付订单
    public function orderFaceUnpay(){
        return $this->hasOne('order_face', 'order_id', 'id')->field('id,order_id,out_trade_no,all_money,last_all_money')->where("is_delete=1 and status in (1)")->order('createtime', 'desc');;
    }

    //订单状态处理
    public static function fetchStatus($status = null){
        $data =  [
            1 => '已预约',
            2 => '用车中',
            3 => '已完成',
            4 => '已取消',
        ];
        if (isset($data[$status])) {
            return $data[$status];
        }else{
            return $data;
        }
    }

    //押金状态处理
    public static function fetchDepositStatus($status = null){
        $data =  [
            0 => '无押金',
            1 => '待退还',
            2 => '已原路退还',
        ];
        if (isset($data[$status])) {
            return $data[$status];
        }else{
            return $data;
        }
    }

    //押金方式处理
    public static function fetchDepositType($deposit_type = null){
        $data =  [
            1 => '门店预授权',
            2 => '门店信用免押',
        ];
        if (isset($data[$deposit_type])) {
            return $data[$deposit_type];
        }else{
            return $data;
        }
    }

    //取车方式
    public static function fetchSendType($status = null){
        $data =  [
            1 => '到店自取',
            2 => '送车上门',
        ];
        if (isset($data[$status])) {
            return $data[$status];
        }else{
            return $data;
        }
    }

    //换车状态处理
    public static function fetchChangeStatus($change_status = null){
        $data =  [
            1 => '未换车',
            2 => '换车中',
            3 => '换车结束',
            4 => '已取消',
        ];
        if (isset($data[$change_status])) {
            return $data[$change_status];
        }else{
            return $data;
        }
    }
    //续租状态处理
    public static function fetchContinueStatus($continue_status = null){
        $data =  [
            1 => '未续租',
            2 => '申请中',
            3 => '已同意',
            4 => '已支付',
            5 => '已拒绝',
        ];
        if (isset($data[$continue_status])) {
            return $data[$continue_status];
        }else{
            return $data;
        }
    }
    //当面付状态处理
    public static function fetchFaceStatus($face_status = null){
        $data =  [
            1 => '无',
            2 => '待支付',
            3 => '已支付',
            4 => '已取消',
        ];
        if (isset($data[$face_status])) {
            return $data[$face_status];
        }else{
            return $data;
        }
    }

    //支付方式
    public static function fetchPayType($pay_type = null){
        $data =  [
            1 => '微信支付',
            2 => '支付宝支付',
        ];
        if (isset($data[$pay_type])) {
            return $data[$pay_type];
        }else{
            return $data;
        }
    }

    //换车申请退款状态
    public static function fetchRefundStatus($refund_status = null){
        $data =  [
            1 => '未申请',
            2 => '审核中',
            3 => '已同意',
            4 => '已拒绝',
        ];
        if (isset($data[$refund_status])) {
            return $data[$refund_status];
        }else{
            return $data;
        }
    }

    //订单退款状态
    public static function fetchOrderRefundStatus($order_refund_status = null){
        $data =  [
            1 => '未申请',
            2 => '审核中',
            3 => '已同意',
            4 => '已拒绝',
        ];
        if (isset($data[$order_refund_status])) {
            return $data[$order_refund_status];
        }else{
            return $data;
        }
    }

    //取消类型处理
    public static function fetchCancelType($cancel_type = null){
        $data =  [
            1 => '行程有变',
            2 => '不想租了',
            3 => '重新下单',
            4 => '价格原因',
            5 => '其他原因',
        ];
        if (isset($data[$cancel_type])) {
            return $data[$cancel_type];
        }else{
            return $data;
        }
    }

	//根据日期计算星期
	public static function week($time){
		$weekarray=["周日","周一","周二","周三","周四","周五","周六"];
		return $weekarray[date("w",$time)];
	}



	//生成唯一退款号
	public static function getOutRefundNo(){
		$out_refund_no = sys_create_orderno(4);
		$order = Order::where('out_refund_no', $out_refund_no)->count();
		if($order){
			Order::getOutRefundNo();
		}
		return $out_refund_no;
	}

}
